﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using Cmn;
using Newtonsoft.Json;
using SH.STD.Model;
using SH.STD.DataProvider;
using System.Web.SessionState;

namespace SH.STD.Web.Handler
{
    /// <summary>
    /// Summary description for ProjectDetail
    /// </summary>
    public class ProjectDetail : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Cache.SetNoStore();
            context.Response.ContentType = "application/json;charset=gb2312";

            switch (context.Request.Params["action"])
            {
                case "loadData":
                    context.Response.Write(ConvertToJsonString(context.Request.Params["id"]));
                    break;
                case "addData":
                    context.Response.Write(AddData(context.Request.Params["jsonDataString"]));
                    break;
                case "removeData":
                    context.Response.Write(RemoveData(context.Request.Params["id"]));
                    break;
                case "updateData":
                    context.Response.Write(UpdateData(context.Request.Params["jsonDataString"]));
                    break;
            }
        }

        private string AddData(string jsonDataString)
        {
            JavaScriptObject patent = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
INSERT INTO ProjectAward ([Name] ,[Memo],[LastModifiedBy])
            VALUES (@Name ,@Memo ,@LastModifiedBy);SELECT @@identity";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("Name",RepsonseInfo.Decode(patent["Name"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(patent["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ID = DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID });
        }
        private string ConvertToJsonString(string id)
        {
            var sql = @"
SELECT a.ProjectID as ProjectID,a.MemberType,b.ID as EmpID,b.Name as EmpName,b.Gender,c.ID as DepID,c.Name as DepName
FROM ProjectMember a
join Employee b
on a.EmployeeID=b.ID
join Department c
on b.DepID=c.ID
WHERE a.ProjectID=@ProjectID and a.status=1";
            var cmdParams = new SqlParameter[]
                {
                    new SqlParameter("ProjectID",id.ToInt()),
                };
            var dt = Cmn.DB.getDataTable(sql, cmdParams);
            return DataAccess.ConvertToJSON(dt, "ProjectMembers");
        }
        private string UpdateData(string jsonDataString)
        {
            JavaScriptObject item = (JavaScriptObject)JavaScriptConvert.DeserializeObject(jsonDataString);
            var sql = @"
Update ProjectAward Set 
Name=@Name,
Memo=@Memo,
LastModifiedBy=@LastModifiedBy
WHERE ID=@ID";
            var cmdParas = new SqlParameter[]
                    {
                        new SqlParameter("ID",RepsonseInfo.Decode(item["ID"].ToString())),
                        new SqlParameter("Name",RepsonseInfo.Decode(item["Name"].ToString())),
                        new SqlParameter("Memo",RepsonseInfo.Decode(item["Memo"].ToString())),
                        new SqlParameter("LastModifiedBy",Cmn.Session.GetUserID()),
                    };
            var ID = DB.getFieldValue(sql, cmdParas);
            return JavaScriptConvert.SerializeObject(new RepsonseInfo { MSG = ID });
        }
        private char RemoveData(string id)
        {
            throw new NotImplementedException();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}